import { Inject, Injectable } from '@nestjs/common'
import { ClientProxy } from '@nestjs/microservices'
import { firstValueFrom } from 'rxjs'
import { CreateServerDto } from './dto/create-server.dto'
import { IndexateResourceDto } from './dto/indexate-resource.dto'
import { Server } from './models/server.model'
import {
  RESOURCES_CLIENT,
  RESOURCES_CREATE_SERVER,
  RESOURCES_REINDEXATE_BOT,
  RESOURCES_REINDEXATE_SERVER,
} from './resources-client.constants'

@Injectable()
export class ResourcesClientService {
  constructor(
    @Inject(RESOURCES_CLIENT) private readonly resources: ClientProxy,
  ) {}

  async createServer(dto: CreateServerDto) {
    return await firstValueFrom(
      this.resources.send<Server, CreateServerDto>(RESOURCES_CREATE_SERVER, dto),
    )
  }

  // see rpc.controller.ts in resources app for details
  async indexateResource(resourceType: 'bot' | 'server', id: string) {
    return await firstValueFrom(
      this.resources.send<void, IndexateResourceDto>(
        resourceType === 'bot' ? RESOURCES_REINDEXATE_BOT : RESOURCES_REINDEXATE_SERVER,
        { id },
      ),
    )
  }
}